<?php
/**
 * Description of session
 *
 * @author sjoerd
 */
class session {
    var $id;
    var $owner;
    var $type;
    var $location;
    var $date;
    var $limit;
    var $result;
    var $hours;
    var $game;
    var $notes;
    function  __construct($id = false) {
        global $database;
        if($id) {
            $sessiondata = $database->sql_array("SELECT * FROM brm_sessions WHERE id = '{$id}'");
            if($sessiondata) {
                $this->id       = $sessiondata[0]['id'];
                $this->owner    = $sessiondata[0]['session_user_id'];
                $this->type     = $sessiondata[0]['session_type'];
                $this->location = $sessiondata[0]['session_location'];
                $this->date     = $sessiondata[0]['session_date'];
                $this->limit    = $sessiondata[0]['session_limit'];
                $this->result   = $sessiondata[0]['session_result'];
                $this->hours    = $sessiondata[0]['session_hours'];
                $this->game     = $sessiondata[0]['session_game'];
                $this->notes    = $sessiondata[0]['session_notes'];
            }
            else
            {
                // Illegal session loaded !
                trigger_error("Session does not exist, quitting...");
                session_destroy();
                exit;
            }
        }
    }
    function get($id) {
        $sessiondata = $database->sql_array("SELECT * FROM brm_sessions WHERE id = '{$id}'");
        if($sessiondata) {
            $this->id       = $sessiondata[0]['id'];
            $this->owner    = $sessiondata[0]['session_user_id'];
            $this->type     = $sessiondata[0]['session_type'];
            $this->location = $sessiondata[0]['session_location'];
            $this->date     = $sessiondata[0]['session_date'];
            $this->limit    = $sessiondata[0]['session_limit'];
            $this->result   = $sessiondata[0]['session_result'];
            $this->hours    = $sessiondata[0]['session_hours'];
            $this->game     = $sessiondata[0]['session_game'];
            $this->notes    = $sessiondata[0]['session_notes'];
        }
        else {
            return false;
        }
    }
    function save()
    {
        global $user;
        global $database;
        if($this->id)
        {
            // Update existing session
            $data['owner']      = $this->owner;
            $data['type']       = $this->type;
            $data['location']   = $this->location;
            $data['date']       = $this->date;
            $data['limit']      = $this->limit;
            $data['result']     = $this->result;
            $data['hours']      = $this->hours;
            $data['game']       = $this->game;
            $data['notes']      = $this->notes;

            $data = $database->escape($data);

            $sql =
            "
                UPDATE brm_sessions 
                    SET 
                        session_user_id     ='{$data['owner']}',
                        session_type        ='{$data['type']}',
                        session_location    ='{$data['location']}',
                        session_date        ='{$data['date']}',
                        session_limit       ='{$data['limit']}',
                        session_result      ='{$data['result']}',
                        session_hours       ='{$data['hours']}',
                        session_game        ='{$data['game']}',
                        session_notes       ='{$data['notes']}'
                    WHERE id = '{$this->id}'
            ";
            $database->sql_exe($sql);
        }
        else
        {
            // insert new session
            $data['owner']      = $this->owner;
            $data['type']       = $this->type;
            $data['location']   = $this->location;
            $data['date']       = $this->date;
            $data['limit']      = $this->limit;
            $data['result']     = $this->result;
            $data['hours']      = $this->hours;
            $data['game']       = $this->game;
            $data['notes']      = $this->notes;

            $data = $database->escape($data);

            $sql =
            "
                INSERT INTO brm_sessions (session_user_id, session_type, session_location, session_date, session_limit, session_result, session_hours, session_game, session_notes)
                VALUES
                ('{$data['owner']}', '{$data['type']}', '{$data['location']}', '{$data['date']}', '{$data['limit']}', '{$data['result']}', '{$data['hours']}', '{$data['game']}', '{$data['notes']}')
            ";
            $database->sql_exe($sql);
        }
    }
}
?>
